//
//  HealthQuickInfoVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/21.
//

#import "HealthQuickInfoVC.h"
#import "HomeQuickInfoCell.h"
#import "HomeDataCell.h"
#import "HomeStressCell.h"
#import "HomeSleepDataCell.h"
#import "HomeMindDataView.h"
#import "HomeIdeaDataCell.h"
#import "HomeQuickCheckVC.h"
@interface HealthQuickInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * listtView;
@property (nonatomic,strong) HomeQuickCheckModel * infoModel;

@end

@implementation HealthQuickInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
    [self requestData];
}

-(void)setSubViewLayout{
    
    self.title = @"报告详情";
    [self.view addSubview:self.listtView];
    [_listtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(NavHeight +20 * FMScaleX, 0, 20 * FMScaleX, 0));
    }];
    
    [(BaseNavController *)[UIViewController findNavigationController]addFullScreenPopBlackListItem:self];
}

-(void)backBtnClick{
    
    [self checkAndPopWithTargetClass:HomeQuickCheckVC.class];
}

- (void)checkAndPopWithTargetClass:(Class)targetClass {
    // 获取导航控制器的视图控制器栈
    NSArray<UIViewController *> *viewControllers = self.navigationController.viewControllers;

    // 判断特定页面是否存在
    BOOL targetExists = NO;
    for (UIViewController *vc in viewControllers) {
        if ([vc isKindOfClass:targetClass]) {
            targetExists = YES;
            break;
        }
    }
    // 根据判断结果执行 pop 操作
    if (targetExists) {
        // 若存在指定页面，pop 到根视图控制器
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        // 若不存在，pop 到上级视图控制器
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)requestData{
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"bh-center/api/invite/HealthDataAndIdAll") params:@{@"id":self.ID} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        self.infoModel = [HomeQuickCheckModel yy_modelWithDictionary:responseObject[@"data"]];
        [self.listtView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    switch (indexPath.row) {
        case 0:
            return  60 * FMScaleX;
            break;
        case 1:
            return  176 * FMScaleX;
            break;
        case 2:
            return  200 * FMScaleX;
            break;
        case 3:
            return  230 * FMScaleX;
            break;
        case 4:
            return  260 * FMScaleX;
            break;
        case 5:
            return  self.infoModel.height;
            break;
        default:
            return 0;
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        HomeQuickInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeQuickInfoCell"];
        [cell setInfoModel:self.infoModel type:1];
        return cell;
    }
    
    if(indexPath.row == 1){
        HomeDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDataCell"];
        [cell setInfoModel:self.infoModel];
        return cell;
    }
    if (indexPath.row == 2) {
        HomeStressCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeStressCell"];
        [cell setInfoModel:self.infoModel.normalYaLi];
        return cell;
    }
    if (indexPath.row == 3) {
        HomeSleepDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSleepDataCell"];
        [cell setInfoModel:self.infoModel.sleepYaLi];
        return cell;
    }
    if (indexPath.row == 4) {
        HomeMindDataView * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeMindDataView"];
        [cell setInfoModel:self.infoModel];
        return cell;
    }
        
    HomeIdeaDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeIdeaDataCell"];
    [cell setInfoModel:self.infoModel];
    return cell;
    
}

-(UITableView *)listtView{
    
    if (!_listtView) {
        _listtView = [[UITableView alloc]init];
        _listtView.delegate = self;
        _listtView.dataSource = self;
        _listtView.showsVerticalScrollIndicator = NO;
        _listtView.showsHorizontalScrollIndicator = NO;
        _listtView.backgroundColor = UIColor.clearColor;
        _listtView.bounces = NO;
        _listtView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listtView registerClass:[HomeQuickInfoCell class] forCellReuseIdentifier:@"HomeQuickInfoCell"];
        [_listtView registerClass:[HomeDataCell class] forCellReuseIdentifier:@"HomeDataCell"];
        [_listtView registerClass:[HomeStressCell class] forCellReuseIdentifier:@"HomeStressCell"];
        [_listtView registerClass:[HomeSleepDataCell class] forCellReuseIdentifier:@"HomeSleepDataCell"];
        [_listtView registerClass:[HomeMindDataView class] forCellReuseIdentifier:@"HomeMindDataView"];
        [_listtView registerClass:[HomeIdeaDataCell class] forCellReuseIdentifier:@"HomeIdeaDataCell"];
        
    }
    return _listtView;
}
@end
